<?php

// 开启协程 runtime
\Swoole\Runtime::enableCoroutine();

//go(function () {
//    $filename = 'write.txt';
//    file_put_contents($filename, "当前时间" . date('Y-m-d H:i:s') . PHP_EOL, FILE_APPEND);
//    echo "write success";
//});

//echo "看看谁先打印" . PHP_EOL;

$http = new Swoole\Http\Server("0.0.0.0", 9501);

$http->on('request', function ($request, $response) {
    go(function() use($request) {
        $filename = 'access.log';
        $data = [
            'date:'     => date('Ymd H:i:s'),
            'get:'      => $request->get,
            'post:'     => $request->post,
            'header:'   => $request->header
        ];
        file_put_contents($filename, json_encode($data) . PHP_EOL, FILE_APPEND);
    });
    // 设置响应头
    $response->header("Content-Type", "text/html; charset=utf-8");
    // 输出的HTML,如果有多段输出用write
    $response->end("<h1>Hello Swoole. #".rand(1000, 9999)."</h1>");
});

$http->start();

// 测试  终端执行 curl http://127.0.0.1:9501?a=hello,world